package cn.itcast.controller.cargo;

import cn.itcast.base.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.ExtCproductService;
import cn.itcast.service.FactoryService;
import cn.itcast.utils.UtilFuns;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ExtCproductService extCproductService;
    @Reference
    private FactoryService factoryService;

    @RequestMapping(name = "pageHelper插件分页查询合同下货物附件",value = "/list")
    public String findAll(String contractId,String contractProductId,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize){

        //根据点击的货物id获取到该货物下的所有附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(contractProductId);

        PageInfo pb = extCproductService.findAll(extCproductExample, pageNum, pageSize);
        request.setAttribute("pb",pb);

        //货物的生产厂家
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria1 = factoryExample.createCriteria();
        criteria1.andCtypeEqualTo("附件");
        List<Factory> list = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",list);

        request.setAttribute("contractProductId",contractProductId);
        request.setAttribute("contractId",contractId);

        return "cargo/extc/extc-list";
    }



    @RequestMapping(name = "附件的新增/修改",value = "/edit")
    public String edit(ExtCproduct extCproduct){

        extCproduct.setCompanyId(company_id);
        extCproduct.setCompanyName(company_name);
        if(UtilFuns.isEmpty(extCproduct.getId())){
            //新增
            extCproductService.save(extCproduct);
        }else {
            //修改
            extCproductService.update(extCproduct);
        }

      return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();
    }



    @RequestMapping(name = "附件的修改页面",value = "/toUpdate")
    public String toUpdate(String id,String contractId,String contractProductId){

        // 根据附件的id获取到附件对象
        ExtCproduct extCproduct = extCproductService.findById(id);
        // 生产附件的厂家带过去
        //货物的生产厂家
        FactoryExample factoryExample = new FactoryExample();
        FactoryExample.Criteria criteria1 = factoryExample.createCriteria();
        criteria1.andCtypeEqualTo("附件");
        List<Factory> list = factoryService.findAll(factoryExample);
        request.setAttribute("extCproduct",extCproduct);
        request.setAttribute("factoryList",list);

        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);
        return "cargo/extc/extc-update";

    }


    @RequestMapping(name = "附件的删除",value = "/delete")
    public String delete(String id,String contractId,String contractProductId){

        //根据附件id删除附件
        extCproductService.delete(id);

        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;

    }






}
